/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package au.edu.swin.swinvehicle.driverlicencetype;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class DriverLicenceTypeManagerBean implements DriverLicenceTypeManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addDriverLicenceType(DriverLicenceTypeDetailedTO to) throws DriverLicenceTypeManagerException {

        try {

            DriverLicenceTypeEntity dlt = new DriverLicenceTypeEntity();
            dlt.setDetailedData(to);
            em.persist(dlt);

        } catch (Exception e) {
            throw new DriverLicenceTypeManagerException("Cannot add Driver Licence Type", e);
        }
    }

    public void editDriverLicenceType(DriverLicenceTypeDetailedTO to) throws DriverLicenceTypeManagerException {
        try {

            DriverLicenceTypeEntity dlt = em.find(DriverLicenceTypeEntity.class, to.getId());
            dlt.setDetailedData(to);
            em.persist(dlt);

        } catch (Exception e) {
            throw new DriverLicenceTypeManagerException("Cannot edit Driver Licence Type", e);
        }

    }

    public void deleteDriverLicenceType(Long id) throws DriverLicenceTypeManagerException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<DriverLicenceTypeTO> getAllDriverLicenceTypes() throws DriverLicenceTypeManagerException {

        List<DriverLicenceTypeTO> results = null;

        try {
            results = new ArrayList<DriverLicenceTypeTO>();
            Query allDriverLicenceTypesQuery = em.createNamedQuery("findAllDriverLicenceTypes");
            List dbResults = allDriverLicenceTypesQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                DriverLicenceTypeEntity dlt = (DriverLicenceTypeEntity) dbResults.get(i);
                results.add(dlt.getData());
            }
        } catch (Exception e) {

            throw new DriverLicenceTypeManagerException("Cannot get all Driver Licence Types", e);

        }
        return results;

    }

    public List<DriverLicenceTypeDetailedTO> getAllDetailedDriverLicenceTypes() throws DriverLicenceTypeManagerException {

        List<DriverLicenceTypeDetailedTO> results = null;

        try {
            results = new ArrayList<DriverLicenceTypeDetailedTO>();
            Query allDriverLicenceTypesQuery = em.createNamedQuery("findAllDriverLicenceTypes");
            List dbResults = allDriverLicenceTypesQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                DriverLicenceTypeEntity dlt = (DriverLicenceTypeEntity) dbResults.get(i);
                results.add(dlt.getDetailedData());
            }
        } catch (Exception e) {

            throw new DriverLicenceTypeManagerException("Cannot get all DriverLicenceTypeDetailedTOs", e);

        }
        return results;

    }

    public DriverLicenceTypeDetailedTO getDriverLicenceType(Long id) throws DriverLicenceTypeManagerException {

        DriverLicenceTypeEntity dlt = null;
        DriverLicenceTypeDetailedTO to = null;
        try {
            dlt = em.find(DriverLicenceTypeEntity.class, id);
            to = dlt.getDetailedData();
        } catch (Exception e) {
            throw new DriverLicenceTypeManagerException("Cannot get Driver Licence Type by id " + id.longValue(), e);
        }
        return to;
    }
    
}
